package com.sgu6.netty.handler;

import com.sgu6.netty.container.Machine;
import com.sgu6.netty.utils.NettyHelper;

import java.util.TimerTask;


/**
 * Created by root on 17-9-23.
 */
public class HeartbeatTask extends TimerTask {
    protected final String TAG = HeartbeatHandler.class.getSimpleName();
    private final String machineNo;
    private final String machineVersion;

    HeartbeatTask(String machineNo, String machineVersion) {
        this.machineNo = machineNo;
        this.machineVersion = machineVersion;
    }


    @Override
    public void run() {
//        for (String str : Machine.status) {
//            Machine.warnInfo = String.format("%s %s ", Machine.warnInfo, str);
//        }

        try {
            String info = machineVersion + "，" + Machine.netType + ":" + Machine.signalStrength + "，" + Machine.heartInfo + "，报警:" + Machine.warnInfo.toString();
            NettyHelper.postMsgWithHeartbeatReq(machineNo, info);
//            Machine.warnInfo = "";
            /**
             if (heartTime >= 5){
             NettyMessageProto.NettyMessage.Builder builderStatus = NettyMessageProto.NettyMessage.newBuilder();
             builderStatus.setHeader(packetBuilder.createHeader(CommandPriorityEnum.普通级.code(),NettyMessageProto.Header.Type.MACHINE_STATUS_REQ,null));
             Map<String, Integer> cmd = new HashMap<>();
             cmd = comProtocol.translateTest2Map(ConnectContainer.COM_MSG);
             if(cmd.size() > 3){

             builderStatus.setBody(Any.pack(packetBuilder.createStatus(MachineContainer.MACHINE_NO,cmd)));
             ctx.writeAndFlush(builderStatus.build());
             heartTime = 0;
             /**
             logger.error("["+MachineContainer.MACHINE_NO+"]向服务器发送了心跳:纵封温度："+cmd.get("vTemp")+",横封温度:"+cmd.get("hTemp")+
             ",谷物温度:"+cmd.get("paddyTemp")+
             ",谷物湿度:"+cmd.get("paddyHumi")+
             ",拉膜长度:"+cmd.get("filmLength")+
             ",步进电机部数:"+cmd.get("motorStep")+
             ",送谷时间:"+cmd.get("paddyFeedTime")+
             ",剩余要打的包数:"+cmd.get("millLeft")+
             ",储米仓重量:"+cmd.get("storeEmptyWeight")+
             ",储米仓当前重量值:"+cmd.get("storeCurrWeight")+
             ",精度电机状态:"+cmd.get("millMotorPrecision")+
             ",掉货检测:"+cmd.get("outputCheck")+
             ",谷物库存:"+cmd.get("goodsStock")+
             ",糠满:"+cmd.get("branStock"));


             }
             }
             */

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}